package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.observers.Subscribers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.App;
import zkk.com.mengqu.R;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.User;
import zkk.com.mengqu.simplepreferences.Share;

/**
 * Created by Administrator on 2017/3/31.
 * 登录界面
 */
public class LoginActivity extends Activity implements View.OnClickListener{
    private Context context;
    private ImageView ivLogin;//返回
    private Button login;//登录
    private TextView register,txLoginCode;//注册、验证码登录
    private EditText etUserPhone, etUserPwd;//用户账号，用户密码
    private ImageButton btnWeChat , btnTencent , btnAliPay;//微信登录，腾讯登录，支付宝登录

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    public void initView(){
        context = getApplicationContext();
        ivLogin = (ImageView)findViewById(R.id.iv_back);
        register = (TextView) findViewById(R.id.tx_register);
        txLoginCode = (TextView) findViewById(R.id.tx_login_code);
        login = (Button) findViewById(R.id.btn_login);

        btnWeChat = (ImageButton)findViewById(R.id.btn_wechat);
        btnTencent = (ImageButton)findViewById(R.id.btn_tencent);
        btnAliPay = (ImageButton)findViewById(R.id.btn_alipay);

        etUserPhone = (EditText) findViewById(R.id.et_user_phone);
        etUserPwd = (EditText) findViewById(R.id.et_user_pwd);

        //添加下划线
        register.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        register.getPaint().setAntiAlias(true);//抗锯齿


        ivLogin.setOnClickListener(this);
        register.setOnClickListener(this);
        txLoginCode.setOnClickListener(this);
//        login.setOnClickListener(this);

        btnWeChat.setOnClickListener(this);
        btnTencent.setOnClickListener(this);
        btnAliPay.setOnClickListener(this);

        login();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tx_login_code:
                startActivity(new Intent(this , LoginCodeActivity.class));
                finish();
                break;
            case R.id.tx_register:
                startActivity(new Intent(this , RegisterActivity.class));
                break;
//            case R.id.btn_login:
//
//                break;
            case R.id.btn_wechat:
                Toast.makeText(this , "微信登录" , Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_tencent:
                Toast.makeText(this , "腾讯登录" , Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_alipay:
                Toast.makeText(this , "支付宝登录" , Toast.LENGTH_LONG).show();
                break;
            default:
                Toast.makeText(this , "未知点击" , Toast.LENGTH_LONG).show();
                break;
        }
    }

    public void login(){
        RxView.clicks(login)
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        String phone = etUserPhone.getText().toString().trim();
                        if (null == phone || phone.length() == 0){
                            Toast.makeText(context, "手机号不能为空", Toast.LENGTH_LONG).show();
                        }
                        return phone.length() > 0;
                    }
                })
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        String pwd = etUserPwd.getText().toString().trim();
                        if (null == pwd || pwd.length() == 0){
                            Toast.makeText(context, "请输入密码", Toast.LENGTH_LONG).show();
                        }
                        return pwd.length() > 0;
                    }
                })
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        String pwd = etUserPwd.getText().toString().trim();
                        if (pwd.length() > 16 || pwd.length() < 8){
                            Toast.makeText(context, "The phone must be between 8 and 16 digits.", Toast.LENGTH_LONG).show();
                        }
                        return pwd.length() < 16 && pwd.length() > 8;
                    }
                })
                .flatMap(new Func1<Void, Observable<User>>() {
                    @Override
                    public Observable<User> call(Void aVoid) {
                        String phone = etUserPhone.getText().toString().trim();
                        String pwd = etUserPwd.getText().toString().trim();
                        return APIManager.setManager()
                                .login(phone, pwd)
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread());
                    }
                })
                .retry(new Func2<Integer, Throwable, Boolean>() {
                    @Override
                    public Boolean call(Integer integer, Throwable throwable) {
                        throwable.printStackTrace();
                        Toast.makeText(context, "登录失败", Toast.LENGTH_LONG).show();
                        return true;
                    }
                })
                .subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        Log.w("onNext", "" + user.getName());
                        Map<String,String> maps = new HashMap<>();
                        maps.put(Share.USERNAME,""+user.getName());
                        maps.put(Share.UID,""+user.getId());
                        maps.put(Share.TOKEN,""+user.getToken());
                        maps.put(Share.SEX,""+user.getSex());
                        maps.put(Share.SEX,""+user.getSex());
                        maps.put(Share.PHONE,""+user.getPhone());
                        maps.put(Share.ICON,""+user.getHead_path());
                        Share.setShare(maps);
                        Toast.makeText(context , "登录成功" , Toast.LENGTH_LONG).show();
                        finish();
                    }
                });;



//        APIManager.setManager()
//                .login(new User(phone , pwd))
//                .enqueue(new Callback<User>() {
//                    @Override
//                    public void onResponse(Call<User> call, Response<User> response) {
//                        Log.w("callBack: ", "" + response);
//                        User user = response.body();
//                        Log.w("onNext", "" + user.getName());
//                        Map<String,String> maps = new HashMap<>();
//                        maps.put(Share.USERNAME,""+user.getName());
//                        maps.put(Share.UID,""+user.getId());
//                        maps.put(Share.TOKEN,""+user.getToken());
//                        maps.put(Share.SEX,""+user.getSex());
//                        maps.put(Share.SEX,""+user.getSex());
//                        maps.put(Share.PHONE,""+user.getPhone());
//                        maps.put(Share.ICON,""+user.getHead_path());
//                        Share.setShare(maps);
//                        Toast.makeText(context , "登录成功" , Toast.LENGTH_LONG).show();
//                        finish();
//                    }
//
//                    @Override
//                    public void onFailure(Call<User> call, Throwable t) {
//
//                    }
//                });
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<User>() {
//                    @Override
//                    public void onCompleted() {
//                        Log.w("onCompleted", "onCompleted");
//                        finish();
//                        Toast.makeText(context , "登录成功" , Toast.LENGTH_LONG).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        Log.w("onError", "onError");
//                        e.printStackTrace();
//                        Toast.makeText(context , "登录失败" , Toast.LENGTH_LONG).show();
//                    }
//
//                    @Override
//                    public void onNext(User user) {
//                        Log.w("onNext", "" + user.getName());
//                        Map<String,String> maps = new HashMap<>();
//                        maps.put(Share.USERNAME,""+user.getName());
//                        maps.put(Share.UID,""+user.getUid());
//                        maps.put(Share.TOKEN,""+user.getToken());
//                        maps.put(Share.SEX,""+user.getSex());
//                        maps.put(Share.ADDRESS,""+user.getAddr());
//                        Share.setShare(maps);
//                    }
//                });





    }
}
